package com.zs.usc.configuration.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zs.usc.common.BaseAuthController;
import com.zs.usc.vo.CurrentUserInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 自动填充处理类
 */
@Component
public class MybatisPlusMetaObjectHandler extends BaseAuthController implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        CurrentUserInfo currentUser = this.getCurrentUser();
        if (Objects.nonNull(currentUser)) {
            this.strictInsertFill(metaObject, "creatorId", String.class, currentUser.getUid());
            this.strictInsertFill(metaObject, "lastUpdateUserId", String.class, currentUser.getUid());
        }


        // 创建时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // 修改时间
        this.strictInsertFill(metaObject, "lastUpdateTime", LocalDateTime.class, LocalDateTime.now());


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 修改时间
        this.strictUpdateFill(metaObject, "lastUpdateTime", LocalDateTime.class, LocalDateTime.now());
        CurrentUserInfo currentUser = this.getCurrentUser();
        if (Objects.nonNull(currentUser)) {
            this.strictInsertFill(metaObject, "lastUpdateUserId", String.class, currentUser.getUid());
        }

    }
}
